import React, {lazy} from "react";
import {Navigate, NonIndexRouteObject} from "react-router-dom";
import {
    InfoCircleOutlined,
    MailOutlined,
    PieChartOutlined, UserOutlined,
} from '@ant-design/icons';
import {t} from "@/language";

interface Meta {
    label?: string;
    icon?: React.ReactNode | string;
}

export interface CustomRouteObject extends NonIndexRouteObject {
    meta?: Meta
    children?: CustomRouteObject[],
    parent?: CustomRouteObject
}

const menuRouter = getMenuRouter()
export default menuRouter

export function getMenuRouter(): CustomRouteObject {
    return {
        path: "system",
        element: <Navigate to='/system/home'/>,
        Component: lazy(() => import("@/Layout")),
        children: getMenuRoutes()
    }
}

/**
 * 获取菜单子路由数组
 */
export function getMenuRoutes(): CustomRouteObject[] {
    return [
        {
            path: "home",
            Component: lazy(() => import('@/views/Home')),
            meta: {
                label: t("home.label"),
                icon: <MailOutlined/>,
            }
        },
        {
            path: "dashboard",
            Component: lazy(() => import('@/views/Dashboard')),
            meta: {
                label: t("dashboard.label"),
                icon: <PieChartOutlined/>,
            }
        },
        {
            path: "user",
            meta: {
                label: t("userManage.label"),
                icon: <UserOutlined/>,
            },
            children: [
                {
                    path: "baseInfo",
                    Component: lazy(() => import("@/views/UserManage")),
                    meta: {
                        label: t("userManage.baseInfo"),
                        icon: <InfoCircleOutlined/>,
                    }
                }
            ]
        }
    ]
}

/**
 * 获取菜单map
 * @param route 当前路由
 * @param map 路由map
 * @param parentPath
 * @param parent 父亲路由
 */
export function getMenuRoutesMap(route?: CustomRouteObject, map: {
    [key: string]: CustomRouteObject
} = {}, parentPath: string = '', parent?: CustomRouteObject): {
    [key: string]: CustomRouteObject
} {
    if (!route) {
        route = getMenuRouter()
    }
    const {children, path} = route
    if (parent) {
        route.parent = parent
    }
    if (typeof path === "string") {
        map[`${parentPath}/${path}`] = route
    }
    if (children && children.length > 0) {
        children.forEach((item: CustomRouteObject) => getMenuRoutesMap(item, map, `${parentPath}/${path}`, route))
    }
    return map
}